package com.zhangx.datastructure.lineartable;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * @ClassName LinearTable
 * @Description TODO
 * @Author zhangx
 * @Date 2020/12/22 9:02
 **/
public class LinearTable {

//    线性表长度
    private int[] arr = new int[10];

//    元素长度
    private int dataSize = 8;

    LinearTable(){
        for (int i = 0; i < dataSize; i++) {
            arr[i] = i+1;
        }
    }

    public int insert(int data,int index){
        if (index >= arr.length || index < 0){
            System.out.println("下标越界"+ index);
            return 1;
        }else {
//            要插入的元素后的每个数据后移一个
            for (int i = dataSize; i >= index; i--) {
                arr[i+1] = arr[i];
            }
            arr[index] = data;
            return 0;
        }
    }

    public int delete(int index){
        if (index >= arr.length || index < 0){
            System.out.println("下标越界"+ index);
            return 1;
        }else {
//            要插入的元素后的每个数据后移一个
            for (int i = index; i <= dataSize; i++) {
                arr[i-1] = arr[i];
            }
            dataSize--;
            return arr[index];
        }
    }

    @Override
    public String toString() {
        return "LinearTable{" +
                "arr=" + Arrays.toString(arr) +
                "},dataSize = " + dataSize;
    }

    public static void main(String[] args) {
        LinearTable linearTable = new LinearTable();
        System.out.println(linearTable);
//        System.out.println("--------插入----------");
//        linearTable.insert(99,0);
        System.out.println("--------删除----------");
        linearTable.delete(3);
        System.out.println(linearTable);

    }
}
